package com.app.utils;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 统一处理json
 */
public class JSONTool {

	public static interface Enum {
		static final String ACCESS_TOKEN= "access_token";			//全部使用登录令牌
		static final String DATA= "data";							//全部json 都使用data 封装
		static final String TOTALPAGE= "totalPage";					//全部json 分页次数
		static final String STATUS= "status";						//全部json 数据状态
		static final String STATUSTEXT= "statusText";				//全部json 数据错误提示信息
		static final String WECHATHOSTV1= "wechatHostV1";			//微信服务地址
		static final String CITY_LIST = "cityList";
		static final String MOBILE_FORMAT = "mobileFormat";			//对比手机号
		static final String FORMAT = "format";						//对比手机号 正则
		static final String VENUEREC_LIST = "venueRecList";			//推荐场馆
		static final String VENUE_BENEFIT_LIST = "venueBenefitList";//惠民场馆
		static final String FIT_TRAIN_LIST = "fitTrainList";		//健身学习
		static final String MALL_GOODS_LIST = "mallGoodsList";		//运动装备
		static final String MATCHATC_LIST = "matchAtcList";			//赛事活动
		static final String CONTENT_LIST = "contentList";			//轮播 / 新闻列表
		static final String MESS_LIST = "messList";					//消息
		static final String COURSE_CLASSFIY = "courseClassfiy";		//健康分类
		static final String FIT_GUIDE_LIST = "fitGuideList";		//健康指导
		static final String LIFE_FIT_LIST = "lifeFitList";			//生活健康
		static final String ACT_LIST = "actList";					//活动列表
		static final String ACT_COUNT = "actCount";					//活动数量
		static final String MATCH_LIST = "matchList";				//赛事列表
		static final String VENUE_EVENT_LIST = "venueEventList";	//场馆类型
		static final String VENUE_DATA_LIST = "venueDataList";		//场馆集合
		static final String ACT_THEME_LIST = "actThemeList";		//场馆主题
		static final String ACT_TIME_LIST = "actTimeList";			//场馆时间
		static final String LEVL_LIST = "levlList";					//场馆运动等级
		static final String ACT_LEADER_LIST = "actLeaderList";		//领队列表
		static final String ACT_CLUB_LIST = "actClubList";			//俱乐部列表
		static final String COMMENT_COUNT = "commentCount";			//评论数量
		static final String COMMENT_LIST = "commentList";			//评论列表
		static final String COMM_LIST = "commList";					//所有圈子
		static final String ATTEND_LIST = "attendList";				//我的圈子
		static final String INVITE_LIST = "inviteList";				//邀请圈子
		static final String TREND_LIST = "trendList";				//动态圈子
		static final String MESSAGE = "message";					//登录消息
		public final String DATE_LIST = "dateList";	        		//活动分类--预约时间
		public final String MGC_LIST = "mgcList";	        		//商城分类
		public final String TOTAL_COUNT = "totalCount";	        	//领导详情--评论数量
		public final String ACT_INFO_LIST = "actInfoList";	        //我的报名活动列表
		public final String SIGNUSERLIST = "signUserList";	        //我的报名活动-报名信息列表
		public final String SIGN_LIST = "signList";	        		//运动圈-邀请详情报名列表
		public final String SIGN_COUNT = "signCount";	        	//邀请详细-用户报名数量
		public final String ITEM_LIST = "itemList";	        		//赛事志愿者列表 /场馆-项目列表
		public final String COUNTY_LIST = "countyList";	        	//场馆-辖区
		public final String VENUE_TYPE = "venueType";	        	//场馆-场馆类型
		public final String ORDERS_TYPE = "ordersType";	        	//场馆-智能排序
		public final String ORDER_CODE = "orderCode";	        	//场馆-订单编号
		public final String DEFAULT_CITY = "defaultCity";	        //默认城市
		public final String SEX_TYPE = "sexType";	        		//性别列表
		public final String CODE_TYPE = "codeType";	        		//证件列表
		public final String CLOTHING_TYPE = "clothingType";	        //服装列表
		public final String BLOOD_TYPE = "bloodType";	        	//血型列表
		public final String PERSON_TYPE = "personType";	        	//人要求列表
		public final String COST_TYPE = "costType";	        		//费用列表
		public final String FAULT_TYPE = "faultType";	        	//故障列表
		public final String MARITAL_TYPE = "maritalStatus";	        //婚姻列表
		public final String EDULEVEL_TYPE = "eduLevel";	        	//学历列表
		public final String INDUSTRY_TYPE = "industryType";	        //行业列表
		public final String PRO_BELM_TYPE = "probelmType";	        //反馈列表
		public final String FEED_BACK_TYPE = "feedBackType";	    //反馈列表
		public final String MOBILE = "mobile";	        			//手机
		public final String VENUE_ORDER_LIST = "venueOrderList";	//我的场馆订单列表
		public final String ORDER_ID = "order_id";					//赛事选手报名 成功返回 id
		public final String TRAIN_COURSE_LIST = "trainCourseList";	//健身培训列表/我的课程
		public final String CATE_LIST = "cateList";					//课程分类
		public final String PUB_ACT_TYPE = "pubActType";			//我报名活动列表 筛分条件
		public final String VENUE_ORDER_TYPE = "venueOrderType";	//我场馆订单列表 筛分条件
		public final String AUDIT_PUB_ACT_TYPE = "auditPubActType";	//我发布活动列表 筛分条件
		public final String AUDITSIGNUSERTYPE = "auditSignUserType";//我发布活动用户列表 筛分条件
		public final String MALLORDERSTATUS = "mallOrderStatus";	//我商城列表 筛分条件
		public final String SPORTS_NEWS_TYPE = "sportsNewsType";	//新闻列表 筛分条件
		public final String SAFETY_GUARANTEE = "safetyGuarantee";	//安全保障 筛分条件
		public final String GLOBAL_SEARCH_TYPE= "globalSearchType";	//全局搜索 筛分条件
		public final String CULTURE_MAP_TYPE= "cultureMapType";	    //地图 筛分条件
		public final String VOLUNTEER_LIST = "volunteerList";		//我志愿者列表
		public final String FIT_MAP_TYPE = "fitMapType";			//健康地图分类列表 类型列表
		public final String FITRES_LIST = "fitresList";				//健康地图分类列表 类型数据列表
		public final String BD_MAP_ICO = "bdMapIco";				//健康地图覆盖物图标
		public final String FITRES_FAULT_LIST = "fitresFaultList";	//我的报修列表
		public final String ORDER_INFO = "orderInfo";				//ali支付信息
		public final String AREA_LIST = "areaList";				    //场馆分类-市
		public final String VENUE_TYPE_LIST = "venueTypeList";		//场馆分类-类别
		public final String SORT_LIST = "sortList";				    //场馆分类-排序
		public final String ACT_TYPE_LIST = "actTypeList";		    //活动分类-类别
		public final String STATUS_LIST = "statusList";		        //活动分类-状态
		public final String COURSE_TYPE_LIST = "courseTypeList";	//课程分类-类别
		public final String COURSE_DATE_LIST = "dateList";	        //课程分类-时间
		public final String QUE_LIST = "queList";	        		//课程详情-问答
		public final String NOTE_LIST = "noteList";	        		//课程详情-笔记
		public final String CLASSIFY_LIST = "classifyList";			//收藏列表
		public final String FAV_LIST = "favList";					//收藏列表(分类)
		public final String SAFETY_LIST = "safetyList";				//保障列表(分类)
		public final String DATA_LIST = "dataList";					//全局搜索结果
		public final String MALL_LIST = "mallList";					//商城列表
		public final String PROV_LIST = "provList";					//省-市-区
		public final String MALL_ADDRESS_LIST = "mallAddressList";	//收货地址列表
		public final String ADDRESS_ID = "address_id";				//收货地址id
		public final String MALL_ORDER_LIST = "mallOrderList";		//商城订单列表
		public final String LOTTERY_URL = "lotteryUrl";				//彩票地址
		public final String CNT = "cnt";							//app消息数量
		public final String DOWN_LINE_MSG = "downLineMsg";			//下载提示信息
		public final String VENUE_MSG = "venueMsg";					//场馆已下线  提示信息
		public final String MATCH_MSG = "matchMsg";					//赛事已下线  提示信息
		public final String ACT_MSG = "actMsg";						//活动已下线  提示信息
		public final String MALL_MSG = "mallMsg";					//商品已下线  提示信息
		public final String COURSE_MSG = "courseMsg";				//课程已下线  提示信息
		public final String APP_VERSION = "appVersion";				//应用版本号
		public final String APP_TITLE = "title";					//应用版本提示标题
		public final String APP_MESSAGE = "message";				//应用版本提示信息
		public final String APP_URL = "url";						//应用版本地址
		public final String LINE_VERSION = "version";				//线上应用版本好
		public final String MOTOR_TYPE = "motorType";				//圈子标题
		public final String DISCOUNT_FREE_INFO = "discountFreeInfo";//免费/优惠总字段
		public final String VENUE_DISCOUNT_MSG = "venueDiscountMsg";//优惠场馆
		public final String VENUE_FREE_MSG = "venueFreeMsg";		//免费场馆
		public final String SQUAREDISCOUNTMSG = "squareDiscountMsg";//优惠广场
		public final String SQUAREFREEMSG = "squareFreeMsg";		//免费广场
		public final String PARKDISCOUNTMSG = "parkDiscountMsg";	//优惠公园
		public final String PARKFREEMSG = "parkFreeMsg";			//免费公园
		public final String STADIUMDISCOUNTMSG = "stadiumDiscountMsg";//优惠体育场
		public final String STADIUMFREEMSG = "stadiumDiscountMsg";	//免费体育场
		public final String IS_PUB = "isPub";						//邀请详情自己发布 0-否 1-是
		public final String NEAR_BY_SERVICE = "nearByService";		//健身地图 热门数据
		public final String ABOUT_SHARE = "aboutShare";				//关于蒙享动
		public final String ABOUT_TITLE = "title";					//关于蒙享动 标题
		public final String ABOUT_MESSAGE = "message";			    //关于蒙享动 内容
		public final String REFUND_ORDER_TYPE = "refundOrderType";	//申请退款情况集合
		public final String REFUND_METHOD = "refundMethod";			//申请退款方式
		public final String CODE = "code";							//token 失效
		public final String MATCH_SIGN_ITEM_NONSUPPORT = "matchSignItemNonsupport";//赛事当前是团体项目
		public final String MATCH_SIGN_GROUP_EXCEL_NONSUPPORT = "matchSignGroupExcelNonsupport";//赛事当前是团体,excel报名
		public final String VENUE_LIST = "venueList";				//场馆集合
		public final String VENUE_COUNT = "venueCount";				//场馆数量
		public final String RES_LIST = "resList";					//资源集合
		public final String COURSE_LIST = "courseList";				//课程集合
		public final String COURSE_COUNT = "courseCount";			//课程数量
		public final String CULTURE_NEWS_TYPE = "cultureNewsType";	//新闻列表筛分条件
		public final String MY_ACT_TYPE = "myActType";				//我的活动列表筛分条件
		public final String MY_VENUE_TYPE = "myVenueType";			//我的场馆列表筛分条件
		public final String COLLECT_TYPE = "collectType";			//我的收藏列表筛分条件
		public final String GOODS_LIST = "goodsList";				//搜索商品集合
		public final String MATCH_TOTAL_COUNT = "matchTotalCount";  //赛事结果总数量
		public final String MATCH_TOTAL_PAGE = "matchTotalPage";    //赛事结果分页数量
		public final String ACT_TOTAL_COUNT = "actTotalCount";      //活动结果总数量
		public final String ACT_TOTAL_PAGE = "actTotalPage";        //活动结果分页数量
		public final String VENUE_TOTAL_COUNT = "venueTotalCount";  //场馆结果总数量
		public final String VENUE_TOTAL_PAGE = "venueTotalPage";    //场馆结果分页数量
		public final String CONTENT_TOTAL_COUNT = "contentTotalCount";  //新闻结果总数量
		public final String CONTENT_TOTAL_PAGE = "contentTotalPage";    //新闻结果分页数量
		public final String COURSE_TOTAL_COUNT = "courseTotalCount";    //课程结果总数量
		public final String COURSE_TOTAL_PAGE = "courseTotalPage";      //课程结果分页数量
		public final String GOODS_TOTAL_COUNT = "goodsTotalCount";      //商品结果总数量
		public final String GOODS_TOTAL_PAGE = "goodsTotalPage";        //商品结果分页数量
		public final String COMMON_USER_LIST = "commonUserList";        //常用联系人列表
		public final String SUPPORT_BUT_NO_OPEN_MSG = "supportButNoOpenMsg";//支付列表占时不开放支付功能(支付宝/微信)提示语
		public final String HINT_MEM_UPDATE_PWD_MSG = "hintMemUpdatePwdMsg";//会员卡支付密码提示修改提示语
		public final String VENUE_MEM_LIST = "venueMemList";   			//会员卡列表
		public final String VENUE_INFO_LIST = "venueInfoList";   		//城市场馆列表
		public final String USER_POINT = "user_point";   		                 //签到分数
		public final String CEN_LONGITUDE = "cenLongitude";   		          //坐标
		public final String CEN_LATITUDE = "cenLatitude";   		          //坐标
		public final String ANS_LIST = "ansList";   		          //课程详情--回复集合
//		public final String DOWN_LINE_MSG = "downLineMsg";   		  //项目下架提示语集合
//		public final String VENUE_MSG = "venueMsg";   		  		  //场馆下架提示语
//		public final String ACT_MSG = "actMsg";   		  		  	  //活动下架提示语
//		public final String COURSE_MSG = "courseMsg";   		  	  //课程下架提示语
		public final String ABOUT_US = "aboutUs";   		  	      //关于网址
		public final String MY_FAVOURITE = "myFavourite";   		  //我的收藏筛分列表
		public final String NODE_LIST = "nodeList";   		       //文化超市筛分-资源栏目
		public final String RES_INFO_TYPE_LIST = "resInfoTypeList";//文化超市筛分-资源类别
		public final String UNIT_LIST = "unitList";				   //文化超市筛分-所属单位
		public final String RES_INFO_LIST = "resInfoList";		   //文化超市列表
		public final String RES_TYPE_LIST = "resTypeList";		   //文化超市分类列表
		public final String SUB_ID = "subId";		   			   //文化超市网页传递-资源id
		public final String TITLE = "title";		   			   //文化超市网页传递-资源title
		public final String VENUE_ACT_ROOM = "venueActRoom";		   //场馆-活动室列表

		static final String CHILDREN = "children";
		static final String DISPLAY_NAME = "display_name";
		static final String CATEGORY = "category";
		static final String UNIT_URL = "unit_url";
		static final String VIDEO_URL = "video_url";
		static final String VIDEO = "video";
		static final String GRUOP = "gruop";
		static final String GRUOP_CHILDRENS = "group_childrens";
//		static final String COUNT = "count";
		static final String SUCCESS = "success";
		static final String VERSION = "version";

	}

	/**
	 * 获取数据的大小
	 * 
	 * @param requestJson
	 * @param allKey 如果有外层 必须传递 没有不需要传递
	 * @param key
	 * @return
	 */
	public static int requestJSONfindNameCount(String requestJson,String allKey, String key) {
		int count = 0;
		if (requestJson == null) {
			return count;
		}

		//因为数据有一层封装 需要在获取外部数据
		if(allKey != null && allKey.length()>0){
			requestJson = JSONTool.requestJSONfindName(requestJson,allKey);
		}

		if(!requestJson.contains(key)) return 0;
		try {
			JSONObject jsonObject = new JSONObject(requestJson);
			if (key == null)
				key = Enum.TOTALPAGE;
			count = jsonObject.getInt(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 网络获取数据 获取str
	 * 
	 * @param requestJson
	 *            网络数据
	 * @param key
	 *            JSONkey //默认
	 * @return course_collection_type_state-list 数据
	 */
	public static String requestJSONfindName(String requestJson, String key) {
		String requestStr = null;
		if (requestJson == null || requestJson.length() == 0) {
			return null;
		}
		try {
			JSONObject jsonObject = new JSONObject(requestJson);
			if (key == null)
				key = Enum.DATA;
			if(requestJson.contains(key))
				requestStr = jsonObject.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return requestStr;
	}

	/**
	 * 获取集合
	 * 
	 * @param <T>
	 * @param JSONStr
	 *            数组JSON
	 * @param clazz
	 *            类
	 * @return
	 */
	public static <T> List<T> jsonTransformClazzs(String JSONStr, Class<T> clazz) {
		List<T> models;
		if(JSONStr == null || JSONStr.length() <3 || JSONStr.equals("[]")  ){
			return null;
		}
		models = JSON.parseArray(JSONStr, clazz);
		return models;
	}

	/**
	 * 网络数据直接转化为集合
	 * 
	 * @param requestJson
	 * @param key
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> requestJSONfindName(String requestJson,
			String key, Class<T> clazz) {
		String requestJSONfindName = requestJSONfindName(requestJson, key);
		return jsonTransformClazzs(requestJSONfindName, clazz);
	}


	/**
	 * 这个方法默认自动去除data 在从data value获取集合 但需要传递下次key
	 * @param requestJson
	 * @param key 集合Key
	 * @param clazz
	 * @param <T>
     * @return
     */
	public static <T> List<T> jsonDefaluTranClazzs(String requestJson,String key, Class<T> clazz) {
		List<T> clazzs = null;
		String requestJSONfindName = requestJSONfindName(requestJSONfindName(requestJson, Enum.DATA), key);
		if(!CommonUtil.isRequestStr(requestJSONfindName)) return null;
		clazzs = jsonTransformClazzs(requestJSONfindName, clazz);
		return clazzs;
	}

	/**
	 * 这个方法在key--null时自动 将可以转为data
	 * @param requestJson
	 * @param key
	 * @param clazz
	 * @param <T>
     * @return
     */
	public static <T> T jsonDefaluTranClazz(String requestJson,String key,Class<T> clazz){
		T t;

		if(requestJson == null || requestJson.isEmpty())
			return null;

		if(key == null){
			key = Enum.DATA;
		}
		String value = requestJSONfindName(requestJson, key);

		if(value == null)
			return null;

		t = requestJSONClazz(value,clazz);

		return t;
	}

	/**
	 * 默认从data 下一级取出对应key下value
	 * @param requestJson
	 * @param key
     * @return
     */
	public static String jsonDefaluTranString(String requestJson,String key){
		return requestJSONfindName(requestJSONfindName(requestJson,Enum.DATA),key);
	}

	/**
	 * 网络数据获取 单独一个JSON map
	 * 
	 * @param requestStr
	 *            如果网络数据位空 返回false
	 * @param key
	 *            boolean null 默认key SUCCESS
	 * @return
	 */
	public static boolean requestJSONBooleanKey(String requestStr, String key) {
		boolean requestValue = false;
		if (requestStr == null) {
			return false;
		}
		try {
			JSONObject jsonObject = new JSONObject(requestStr);
			if (key == null) {
				key = Enum.SUCCESS;
			}
			requestValue = jsonObject.getBoolean(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return requestValue;
	}

	/**
	 * 网络数据获取 单独一个JSON 返回clazz 无key
	 * 
	 * @param <T>
	 * @param requestStr
	 *            如果网络数据位空 返回null
	 * @return
	 */
	public static <T> T requestJSONClazz(String requestStr, Class<T> clazz) {
		if (requestStr == null) {
			return null;
		}
		try {
			return JSON.parseObject(requestStr, clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 网络数据获取 单独一个JSON key获取字符串
	 * 
	 * @param requestStr
	 *            如果网络数据位空 返回null
	 * @param key
	 *            String
	 * @return
	 */
	public static String requestJSONStringKey(String requestStr, String key) {
		String requestValue = null;
		if (requestStr == null) {
			return null;
		}
		try {
			JSONObject jsonObject = new JSONObject(requestStr);
			requestValue = jsonObject.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return requestValue;
	}


	/**
	 * 数据状态返回  0-成功，1-失败
	 * @param requestStr
     * @return
     */
	public static boolean isStatus(String requestStr){
		if (requestStr == null) {
			return false;
		}
		int status = requestJSONfindNameCount(requestStr,null, Enum.STATUS);
		return (status == 0);
	}

	/**
	 * 错误试 错误信息
	 * @para requestStr
     * @return
     */
	public static String errorHint(String requestStr){
		String hint = "";
		if (CommonUtil.isRequestStr(requestStr)) {
			hint = requestJSONfindName(requestStr,Enum.STATUSTEXT);
		}
		return hint;
	}

}
